<html>
<head><meta charset="utf-8"><title>warn on casting fn to non-usize integer (edition change?) · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/warn.20on.20casting.20fn.20to.20non-usize.20integer.20(edition.20change.3F).html">warn on casting fn to non-usize integer (edition change?)</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="232471573"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/warn%20on%20casting%20fn%20to%20non-usize%20integer%20%28edition%20change%3F%29/near/232471573" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/warn.20on.20casting.20fn.20to.20non-usize.20integer.20(edition.20change.3F).html#232471573">(Mar 30 2021 at 18:11)</a>:</h4>
<p>a while back I opened <a href="https://github.com/rust-lang/rust/issues/81686">https://github.com/rust-lang/rust/issues/81686</a> with the idea to make it a warning to use <code>as</code> to cast a function item to any integer other than usize, with the implicit hope that it could be made into an error someday. the example given is a pretty good motivator, IMO :P Does anybody from the lang team agree with this idea? there's already a patch to implement the warning, and there's still time to make it into an edition change, if desired</p>



<a name="232471686"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/warn%20on%20casting%20fn%20to%20non-usize%20integer%20%28edition%20change%3F%29/near/232471686" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/warn.20on.20casting.20fn.20to.20non-usize.20integer.20(edition.20change.3F).html#232471686">(Mar 30 2021 at 18:12)</a>:</h4>
<p><span class="user-mention" data-user-id="256342">@bstrie</span> We actually just discussed that today.</p>



<a name="232471711"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/warn%20on%20casting%20fn%20to%20non-usize%20integer%20%28edition%20change%3F%29/near/232471711" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/warn.20on.20casting.20fn.20to.20non-usize.20integer.20(edition.20change.3F).html#232471711">(Mar 30 2021 at 18:12)</a>:</h4>
<p>To remove the time-sensitivity: this doesn't need to be an edition change, we can introduce lints without waiting for an edition.</p>



<a name="232471814"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/warn%20on%20casting%20fn%20to%20non-usize%20integer%20%28edition%20change%3F%29/near/232471814" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/warn.20on.20casting.20fn.20to.20non-usize.20integer.20(edition.20change.3F).html#232471814">(Mar 30 2021 at 18:13)</a>:</h4>
<p>There's some back-and-forth about which semantics we want. I'm in favor of the PR as written, and I would additionally be in favor of a lint that requires explicitly turning a function into a pointer before turning it into an integer (even if the integer type is usize or u64).</p>



<a name="232522264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/warn%20on%20casting%20fn%20to%20non-usize%20integer%20%28edition%20change%3F%29/near/232522264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/warn.20on.20casting.20fn.20to.20non-usize.20integer.20(edition.20change.3F).html#232522264">(Mar 31 2021 at 01:24)</a>:</h4>
<p>I don't understand the value in requiring that you cast it to a pointer of some whatever type before you can cast it to usize</p>



<a name="232523042"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/warn%20on%20casting%20fn%20to%20non-usize%20integer%20%28edition%20change%3F%29/near/232523042" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/warn.20on.20casting.20fn.20to.20non-usize.20integer.20(edition.20change.3F).html#232523042">(Mar 31 2021 at 01:35)</a>:</h4>
<p><code>std::mem::size_of::&lt;i32&gt; as u64</code> I believe is a reason.</p>



<a name="232523515"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/warn%20on%20casting%20fn%20to%20non-usize%20integer%20%28edition%20change%3F%29/near/232523515" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/warn.20on.20casting.20fn.20to.20non-usize.20integer.20(edition.20change.3F).html#232523515">(Mar 31 2021 at 01:41)</a>:</h4>
<p>well, the current proposal to restrict only to usize (and u64?) prevents accidental <code>i8::max as u32</code>, but not <code>i8::max as usize</code>. requiring the user to first cast to a pointer would completely eliminate the risk of accidentally casting a function to any int, though it would be more disruptive to existing code</p>



<a name="232524147"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/warn%20on%20casting%20fn%20to%20non-usize%20integer%20%28edition%20change%3F%29/near/232524147" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/warn.20on.20casting.20fn.20to.20non-usize.20integer.20(edition.20change.3F).html#232524147">(Mar 31 2021 at 01:49)</a>:</h4>
<p><strong>very</strong> disruptive to both current and future code</p>



<a name="232554148"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/warn%20on%20casting%20fn%20to%20non-usize%20integer%20%28edition%20change%3F%29/near/232554148" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/warn.20on.20casting.20fn.20to.20non-usize.20integer.20(edition.20change.3F).html#232554148">(Mar 31 2021 at 08:33)</a>:</h4>
<p>we don't allow casting a reference directly to an integer, this feels very similar to me. I'm very much in the boat of we should require casts to pointers first</p>



<a name="232554931"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/warn%20on%20casting%20fn%20to%20non-usize%20integer%20%28edition%20change%3F%29/near/232554931" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/warn.20on.20casting.20fn.20to.20non-usize.20integer.20(edition.20change.3F).html#232554931">(Mar 31 2021 at 08:41)</a>:</h4>
<p>I'd definitely like a more agressive lint for casting <em>function items</em> than for casting general pointers.  <code>u8::max as usize</code> should probably lint, but I agree casting a <code>*const i32</code> to <code>u64</code> probably shouldn't.</p>



<a name="232562684"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/warn%20on%20casting%20fn%20to%20non-usize%20integer%20%28edition%20change%3F%29/near/232562684" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/warn.20on.20casting.20fn.20to.20non-usize.20integer.20(edition.20change.3F).html#232562684">(Mar 31 2021 at 09:45)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/213817-t-lang/topic/warn.20on.20casting.20fn.20to.20non-usize.20integer.20.28edition.20change.3F.29/near/232554148">said</a>:</p>
<blockquote>
<p>we don't allow casting a reference directly to an integer, this feels very similar to me. I'm very much in the boat of we should require casts to pointers first</p>
</blockquote>
<p>we dont have "raw function pointers" though so the analogy doesn't really hold</p>



<a name="232567626"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/warn%20on%20casting%20fn%20to%20non-usize%20integer%20%28edition%20change%3F%29/near/232567626" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/warn.20on.20casting.20fn.20to.20non-usize.20integer.20(edition.20change.3F).html#232567626">(Mar 31 2021 at 10:27)</a>:</h4>
<p>I thought the issue is not with casting function pointers, it is with casting "function item zsts". If you go through a function pointer cast first, then it is obvious you are trying to cast the function pointer and not something else</p>



<a name="232607665"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/warn%20on%20casting%20fn%20to%20non-usize%20integer%20%28edition%20change%3F%29/near/232607665" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/warn.20on.20casting.20fn.20to.20non-usize.20integer.20(edition.20change.3F).html#232607665">(Mar 31 2021 at 15:07)</a>:</h4>
<p>Isn't this kind of verbose though? It would be great if this could be written <code>foo as fn(_) -&gt; _ as usize</code> but I don't think that syntax works</p>



<a name="232607825"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/warn%20on%20casting%20fn%20to%20non-usize%20integer%20%28edition%20change%3F%29/near/232607825" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/warn.20on.20casting.20fn.20to.20non-usize.20integer.20(edition.20change.3F).html#232607825">(Mar 31 2021 at 15:08)</a>:</h4>
<p>I assume <code>foo as *const _ as usize</code> doesn't work because I have no idea what the <code>_</code> would get inferred to</p>



<a name="232608187"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/warn%20on%20casting%20fn%20to%20non-usize%20integer%20%28edition%20change%3F%29/near/232608187" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/warn.20on.20casting.20fn.20to.20non-usize.20integer.20(edition.20change.3F).html#232608187">(Mar 31 2021 at 15:10)</a>:</h4>
<p>In particular, if there isn't a way to do this with underscores then it may make it impossible (or at least very difficult) to write macros that convert a function with a random signature to a <code>usize</code>, since you would have to first render the signature as an <code>fn</code> type</p>



<a name="232623024"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/warn%20on%20casting%20fn%20to%20non-usize%20integer%20%28edition%20change%3F%29/near/232623024" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/warn.20on.20casting.20fn.20to.20non-usize.20integer.20(edition.20change.3F).html#232623024">(Mar 31 2021 at 16:35)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/213817-t-lang/topic/warn.20on.20casting.20fn.20to.20non-usize.20integer.20.28edition.20change.3F.29/near/232607825">said</a>:</p>
<blockquote>
<p>I assume <code>foo as *const _ as usize</code> doesn't work because I have no idea what the <code>_</code> would get inferred to</p>
</blockquote>
<p>You can give it an arbitrary <code>Sized</code> pointee: <code>foo as *const () as usize</code>, or even some custom <code>code</code> type: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=794ee23d87d5a94f102479990a4a9620">Playground</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>